package com.sky.controller.admin;

import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@Slf4j
@Api(tags = "套餐相关接口")
@RequestMapping("/admin/setmeal")
public class SetmealController {

    @Autowired
    private SetmealService setmealService;

    @ApiOperation("分页查询")
    @GetMapping("/page")
    public Result<PageResult> PageCheck(SetmealPageQueryDTO setmealPageQueryDTO){
        PageResult pageResult=setmealService.PageCheck(setmealPageQueryDTO);
        return Result.success(pageResult);

    }

    @ApiOperation("起售,停售套餐")
    @PostMapping("/status/{status}")
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)

    public Result StartorStop(@PathVariable("status") Integer status,Long id){
         setmealService.StartorStop(status,id);
        return Result.success();
    }


    @ApiOperation("批量删除")
    @DeleteMapping
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public Result SetmealDelete(@RequestParam List<Integer> ids){
         setmealService.SetmealDelete(ids);
        return Result.success();
    }

    @ApiOperation("根据id查询套餐")
    @GetMapping("/{id}")
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)

    public Result<SetmealVO> SelectById(@PathVariable("id") Integer id){
        SetmealVO setmealVO=setmealService.SelectById(id);
        return Result.success(setmealVO);
    }

    @ApiOperation("新增套餐")
    @PostMapping
    @CacheEvict(cacheNames = "setmealCache",key = "#setmealDTO.categoryId")
    public Result InsertSetmeal(@RequestBody SetmealDTO setmealDTO){

        setmealService.InsertSetmeal(setmealDTO);

        return Result.success();
    }

    @ApiOperation("修改套餐")
    @PutMapping
    @CacheEvict(cacheNames = "setmealCache",key = "#setmealDTO.categoryId")
    public Result UpdateSetmeal(@RequestBody SetmealDTO setmealDTO){

        setmealService.UpdateSetmeal(setmealDTO);

        return Result.success();
    }

}
